<template>
	<view class="container p30">
		<!-- ======================================订单状态====================================== -->
		<view class="status">
			<text>{{detail.state_text}}</text>
		</view>
		<!-- ======================================购买商品====================================== -->
		<view class="goods">
			<view class="good">
				<image 
					:src="detail.goods.goods_images[0]" mode="">
				</image>
				<view class="goods_msg">
					<view class="goods_name">{{detail.goods.goods_name}}</view>
					<view class="goods_price">￥<text>{{detail.goods.goods_price}}</text></view>
				</view>
				<view class="goods_call" @click="make_phone">联系{{type==1?'卖家':'买家'}}</view>
			</view>
		</view>
		<!-- ======================================收货地址====================================== -->
		<view class="address">
			<view class="addre">收货地址
				<text style="margin: 0 30rpx;">
					{{detail.user_address.receive_name}}
				</text>
				<text>{{detail.user_address.receive_tel}}</text>
			</view>
			<view class="add_msg">
				{{detail.user_address.address}}
			</view>
			<view class="add_msg" style="margin-top: 30rpx;">
				<text style="color: #000;">商品自提地址:</text>{{detail.address}}
			</view>
		</view>
		<!-- ======================================订单信息====================================== -->
		<view class="order_msg">
			<view class="title">订单信息</view>
			<view class="msg">
				<view class="money">交易方式：</view>
				<view class="price">{{detail.deal_type}}</view>
			</view>
			<view class="msg">
				<view class="money">实付金额：</view>
				<view class="price"><text>{{detail.total_amount}}</text>元</view>
			</view>
			<view class="msg">
				<view class="money">支付方式：</view>
				<view class="price"><span>微信支付</span></view>
			</view>
			<view class="msg">
				<view class="money">订单编号：</view>
				<view class="price">{{detail.order_sn}}</view>
			</view>
			<view class="msg">
				<view class="money">创建时间：</view>
				<view class="price">{{detail.created_at}}</view>
			</view>
			<view class="msg" v-if="detail.state==3||detail.state==4">
				<view class="money">付款时间：</view>
				<view class="price">{{detail.pay_at}}</view>
			</view>
		</view>
		<!-- ======================================操作按钮====================================== -->
		<view class="btn">
			<view>订单疑问</view>
			<view @click="order_state(detail.state,1)" v-if="type==1">
				{{detail.state==1?'去支付':detail.state==2?'确认收货':detail.state==3?'去评价':'查看评价'}}
			</view>
			<view @click="order_state(detail.state,2)" 
				v-if="type==2&&detail.state==4">
				查看评价
			</view>
		</view>
		<!-- ======================================评价组件====================================== -->
		<view class="comment_pop" v-if="commentFlag">
			<view class="pop">
				<textarea v-model="message" value="" maxlength='50' placeholder="对卖家,对商品进行评价吧(0/50)" />
				<view class="uploadPic">
					<image :src="detail.goods.goods_images[0]" mode=""></image>
				</view>
				<view class="fabuBtn" @click="send_message">发布</view>
			</view>
		</view>
	</view>
</template>

<script>
	// 引入封装的请求
	import request from '@/utils/request.js'
	export default{
		data() {
			return{
				// 订单id
				id:null,
				// 卖家(卖出)2/买家(买到)1 
				type:1,
				commentFlag: false, // 评论组件展示
				// 订单详情
				detail:{},
				// 订单的评价
				message:null
			}
		},
		onLoad(options) {
			console.log(options.id)
			let {id,type} = options
			this.id = id
			this.type = type
			this.get_order_detail()
		},
		onShow() {
			
		},
		methods:{
			// 联系卖家/买家
			make_phone(){
				let phone = null
				if(this.type==1){
					phone = this.detail.user.phone
				}else{
					phone = this.detail.user_address.receive_tel
				}
				uni.makePhoneCall({
					phoneNumber:phone
				})
			},
			// 根据订单状态 判断
			order_state(state,type){
				if(type == 1){
					if(state == 1){
						// 调用支付的接口
						this.pay()
						return false
					}
					if(state == 2){
						// 确认收货
						this.comfirm_receipt()
						return false
					}
					if(state == 3){
						this.commentFlag = true
						return false
					}
					uni.navigateTo({
						url:'/neighbor/market/order/orderComment?id='+this.id+'&type=1'
					})
				}else{
					uni.navigateTo({
						url:'/neighbor/market/order/orderComment?id='+this.id+'&type=2'
					})
				}
			},
			// 确认收货
			comfirm_receipt(){
				let that = this
				request.post({
					url:'Mini/circle/flea/orderComplete',
					data:{
						order_id:this.id
					}
				}).then(res=>{
					uni.showToast({
						title:res.msg
					})
					if(res.code==200){
						that.get_order_detail()
					}
				})
			},
			// 订单评价
			send_message(){
				let that = this
				request.post({
					url:'Mini/circle/flea/comment',
					data:{
						order_id:that.id,
						content:that.message,
						images:''
					},
				}).then(res=>{
					console.log(res)
					if(res.code==200){
						uni.showToast({
							title:'评论成功',
							success() {
								that.commentFlag = false
								uni.navigateTo({
									url:'/neighbor/market/order/orderComment?id='+that.id
								})
							}
						})
					}
				})
			},
			// 付款
			pay(){
				let that = this
				// 获取支付参数 
				request.get('Mini/circle/flea/getPayConfig',{
					order_sn:that.detail.order_sn
				}).then(res=>{
					if(res.code==200){
						let paydata = res.data
						uni.requestPayment({
						    provider: 'wxpay',
						    timeStamp: paydata.timeStamp,
						    nonceStr: paydata.nonceStr,
						    package: paydata.package,
						    signType: paydata.signType,
						    paySign: paydata.paySign,
						    success: function (res) { 
								uni.showToast({
									title:"支付成功",
									icon:'none'
								});
								
								uni.redirectTo({
									url:'/neighbor/market/mine/buy/index'
								})
						    },
						    fail: function (err) {
						        console.log('fail:' + JSON.stringify(err));
						    }
						});
					}
				})
			},
			// 获取订单详情
			get_order_detail(){
				let that = this
				request.get('Mini/circle/flea/order/'+this.id,{
					id:this.id
				}).then(res=>{
					console.log(res)
					if(res.code==200){
						that.detail = res.data
					}
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	.status{
		width: 100%;
		height: 36rpx;
		font-size: 36rpx;
		line-height: 36rpx;
		font-family: Hiragino Sans GB;
		font-weight: bold;
		color: #333333;
		border-left: 4rpx solid rgba(252, 130, 97, 1);
		margin-bottom: 30rpx;
		&>text{
			margin-left: 30rpx;
		}
	}
	.goods{
		width: 100%;
		padding: 24rpx 0;
		box-sizing: border-box;
		border-bottom: 1rpx solid rgba(220, 220, 220, 1);
		.good{
			display: flex;
			flex-direction: row;
			justify-content: flex-start;
			align-items: center;
			&>image{
				display: block;
				height: 100rpx;
				width: 100rpx;
			}
			.goods_msg{
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				margin: 0 34rpx;
				font-size: 26rpx;
				font-family: Hiragino Sans GB;
				font-weight: normal;
				color: #333333;
				.goods_name{
					width: 400rpx;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
				.goods_price{
					color: #B83D42;
				}
			}
			.goods_call{
				font-size: 26rpx;
				font-family: Hiragino Sans GB;
				font-weight: normal;
				color: #4BA7E9;
			}
		}
	}
	.address{
		width: 100%;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #333333;
		padding: 0rpx 0 30rpx;
		box-sizing: border-box;
		border-bottom: 1rpx solid rgba(220, 220, 220, 1);
		.addre{
			margin: 30rpx 0;
		}
		.add_msg{
			color: #999999;
		}
	}
	.order_msg{
		margin-top: 30rpx;
		width: 100%;
		.title{
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #333333;
			margin-bottom: 30rpx;
		}
		.msg{
			display: flex;
			flex-direction: row;
			justify-content: flex-start;
			align-items: center;
			margin: 30rpx 0;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #999999;
			.money{
				margin-right: 84rpx;
			}
			.price{
				flex: 1;
				&>text{
					color: rgba(184, 61, 66, 1);
				}
			}
		}
	}
	.btn{
		display: flex;
		flex-direction: row;
		justify-content: flex-end;
		margin-top: 60rpx;
		&>view{
			width: 152rpx;
			height: 52rpx;
			border: 1rpx solid #333333;
			text-align: center;
			font-size: 28rpx;
			font-family: Hiragino Sans GB;
			font-weight: normal;
			color: #333333;
			line-height: 52rpx;
			border-radius: 10rpx;
			margin-left: 30rpx;
		}
	}
	.comment_pop{
		position: fixed;
		left: 0;
		top: 0;
		height: 100vh;
		width: 100vw;
		background-color: rgba(0,0,0,0.6);
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		.pop{
			margin-top: -100rpx;
			width: calc(100vw - 60rpx);
			height: 558rpx;
			background-color: #fff;
			padding: 61rpx 35rpx 44rpx;
			box-sizing: border-box;
			border-radius: 5px;
			&>textarea{
				width: 100%;
				height: 200rpx;
			}
			.uploadPic{
				height: 104rpx;
				width: 104rpx;
				&>image{
					display: block;
					width: 100%;
					height: 100%;
				}
			}
			.fabuBtn{
				height: 70rpx;
				width: 590rpx;
				border-radius: 35rpx;
				background: linear-gradient(3deg, #FC9E00 0%, #FBB10D 76%);
				line-height: 70rpx;
				text-align: center;
				margin: 60rpx auto 0;
				font-size: 30rpx;
				font-family: Hiragino Sans GB;
				font-weight: normal;
				color: #FFFFFF;
			}
		}
	}
	
	
	
	
	
	
</style>
